package cn.fzc.yipai.service.impl;

import cn.fzc.yipai.entity.NewGoodsInfo;
import cn.fzc.yipai.entity.PageInfo;
import cn.fzc.yipai.entity.Response;
import cn.fzc.yipai.entity.StoreInfo;
import cn.fzc.yipai.service.GoodsService;
import cn.fzc.yipai.utils.HttpUtils;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: fu.zc
 * @Date: 2021/6/14 23:06
 * @Description:
 */
@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {


    /**
     * 商品列表[上午场]
     */
    public static final String UP_LIST_URL = "https://www.yihui-xh.xyz/yip/otc/advertise/getgoodssellerlist/6/1/-1/1/100";
    /**
     * 商品列表[下午场]
     */
    public static final String MID_LIST_URL = "https://www.yihui-xh.xyz/yip/otc/advertise/getgoodssellerlist/6/1/-1/1/100";
    /**
     * 商品列表[晚上]
     */
    public static final String DOWN_LIST_URL = "https://www.yihui-xh.xyz/yip/otc/advertise/getgoodssellerlist/6/1/-1/1/100";

    public static final String GOODS_LIST_URL = "https://www.yihui-xh.xyz/yip/otc/advertise/getgoodssellerlist/%s/1/-1/1/300";

    /**
     * 获取活动场次
     */
    public static final String GET_STORES_INFOS = "https://www.yihui-xh.xyz/yip/otc/advertise/getstoresinfos/1/100";

    /**
     * 下单接口
     */
    public static final String KILL_URL = "https://www.yihui-xh.xyz/yip/otc/order/addorderNew";


    private final HttpUtils httpUtils;

    @Autowired
    public GoodsServiceImpl(HttpUtils httpUtils) {
        this.httpUtils = httpUtils;
    }


    public static void main(String[] args) {

        DateTime parse = DateUtil.parse("10:30:00", DatePattern.NORM_TIME_PATTERN);
        parse.isAfter(new Date());


        if (parse.isAfter(new Date())) {
            System.out.println("ok");
        } else {
            System.out.println("false");
        }


    }

    @SneakyThrows
    @Override
    public List<NewGoodsInfo> getLocalGoodsList() {

        String fileToString = FileUtils.readFileToString(new File("/Users/fzc/Desktop/paipai.json"));
//        String fileToString = FileUtils.readFileToString(new File("C:\\Users\\admin\\Desktop\\tmp\\pai.json"));

        List<NewGoodsInfo> newGoodsInfos = JSON.parseArray(fileToString, NewGoodsInfo.class);


        newGoodsInfos = newGoodsInfos.stream().sorted(Comparator.comparingInt(t -> t.getGoodsInfoVO().getBasePrice())).collect(Collectors.toList());


        Collections.reverse(newGoodsInfos);

        return newGoodsInfos.subList(0, 150);
    }

    @Override
    public List<NewGoodsInfo> getGoodsList(String token) {


        return null;
    }

    @Override
    public List<NewGoodsInfo> getGoodsList(StoreInfo storeInfo, String token) {
        String result = httpUtils.executeGet(String.format(GOODS_LIST_URL, storeInfo.getId()), token);
        if (StringUtils.isNotBlank(result)) {
            Response<PageInfo<NewGoodsInfo>> pageInfoResponse = JSON.parseObject(result, new TypeReference<Response<PageInfo<NewGoodsInfo>>>() {
            });
            return pageInfoResponse.getResult().getList();
        }

        return null;
    }

    @Override
    public List<StoreInfo> getStoreInfoList(String token) {
        String result = httpUtils.executeGet(GET_STORES_INFOS, token);
        if (StringUtils.isNotBlank(result)) {
            Response<PageInfo<StoreInfo>> pageInfoResponse = JSON.parseObject(result, new TypeReference<Response<PageInfo<StoreInfo>>>() {
            });
            return pageInfoResponse.getResult().getList();
        }
        return null;
    }

    @Override
    public void kill(String token) {


    }

    @Override
    public void kill(String userName, String pwd) {

    }
}
